<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">

<title>modules_and_classes - RDoc Documentation</title>


<script src="../js/navigation.js" defer></script>
<script src="../js/search.js" defer></script>
<script src="../js/search_index.js" defer></script>
<script src="../js/searcher.js" defer></script>
<script src="../js/darkfish.js" defer></script>

<script src="../js/jquery-3.2.0.min.js"></script>

<script src="../js/vue.min.js"></script>
<script src="../js/js.cookie.min.js"></script>

<link href="../css/fonts.css" rel="stylesheet">
<link id='rdoccss' href="../css/rdoc.css" rel="stylesheet">
<link href="../css/carbon17.css" rel="stylesheet">

<script type="text/javascript">
  var rdoc_rel_prefix = "../";
  var index_rel_prefix = "../";
  var darkModeCsseHref = "../css/rdoc-dm.css"
  var defaultModeCssHref = "../css/rdoc.css"
  // var cssDarkmode = Cookies.get('darkmode');
  
  if( Cookies.get("darkmode") == "true") {
	$('#rdoccss').attr("href", darkModeCsseHref);
}

//  https://cssdeck.com/blog/simple-jquery-stylesheet-switcher/

document.write('<style type="text/css">body{display:none}</style>');

</script>


</head>
<body id="top" role="document" class="file">
  <!-- this is page.html -->
  <div id='actionbar' >
    <div class='wrapper mdiv'>
      <ul class='grids g0'></ul>
    </div> 
    <!-- VERSION HEADER for 3.3.0-preview2 NOT FOUND -->
  </div> <!-- end action bar -->


 <div class='wrapper hdiv'>

 


<nav id='vapp' role="navigation">
  <div id="project-navigation">
    <div id="home-section" role="region" title="Quick navigation" class="nav-section">
  <h2><a href="../index.html" rel="home">Home</a></h2>

  <div id="table-of-contents-navigation"  >
    <a href="../table_of_contents.html#pages">Pages</a>
    <a href="../table_of_contents.html#classes">Classes</a>
    <a href="../table_of_contents.html#methods">Methods</a>
  </div>
</div>

    <div id="search-section" role="search" class="project-section initially-hidden">
  <form action="#" method="get" accept-charset="utf-8">
    <div id="search-field-wrapper">
      <input id="search-field" role="combobox" aria-label="Search"
             aria-autocomplete="list" aria-controls="search-results"
             type="text" name="search" placeholder="Search" spellcheck="false"
             title="Type to search, Up and Down to navigate, Enter to load">
    </div>

    <ul id="search-results" aria-label="Search Results"
        aria-busy="false" aria-expanded="false"
        aria-atomic="false" class="initially-hidden"></ul>
  </form>
</div>

  </div>

  
<div class="nav-section">
  <h3>Table of Contents</h3>

  <ul class="link-list" role="directory">
    <li><a href="#label-Modules">Modules</a>
    <li><a href="#label-Module+Definition">Module Definition</a>
    <li><a href="#label-Nesting">Nesting</a>
    <li><a href="#label-Scope">Scope</a>
    <li><a href="#label-self"><code>self</code></a>
    <li><a href="#label-Constants">Constants</a>
    <li><a href="#label-Methods">Methods</a>
    <li><a href="#label-Visibility">Visibility</a>
    <li><a href="#label-alias+and+undef"><code>alias</code> and <code>undef</code></a>
    <li><a href="#label-Classes">Classes</a>
    <li><a href="#label-Defining+a+class">Defining a class</a>
    <li><a href="#label-Inheritance">Inheritance</a>
    <li><a href="#label-Singleton+Classes">Singleton Classes</a>
  </ul>
</div>

  <button id='toggleThing' @click="toggleNav()" >Show/hide navigation</button>

  <div id="project-metadata">
   <div :class="isOpen ? 'block' : 'hidden' " id='toggleMe'>
    
<div id="fileindex-section" class="nav-section">
  <h3>Pages</h3>

  <ul class="link-list">
    <li><a href="../NEWS/NEWS-1_8_7.html">NEWS-1.8.7</a>
    <li><a href="../NEWS/NEWS-1_9_1.html">NEWS-1.9.1</a>
    <li><a href="../NEWS/NEWS-1_9_2.html">NEWS-1.9.2</a>
    <li><a href="../NEWS/NEWS-1_9_3.html">NEWS-1.9.3</a>
    <li><a href="../NEWS/NEWS-2_0_0.html">NEWS-2.0.0</a>
    <li><a href="../NEWS/NEWS-2_1_0.html">NEWS-2.1.0</a>
    <li><a href="../NEWS/NEWS-2_2_0.html">NEWS-2.2.0</a>
    <li><a href="../NEWS/NEWS-2_3_0.html">NEWS-2.3.0</a>
    <li><a href="../NEWS/NEWS-2_4_0.html">NEWS-2.4.0</a>
    <li><a href="../NEWS/NEWS-2_5_0.html">NEWS-2.5.0</a>
    <li><a href="../NEWS/NEWS-2_6_0.html">NEWS-2.6.0</a>
    <li><a href="../NEWS/NEWS-2_7_0.html">NEWS-2.7.0</a>
    <li><a href="../NEWS/NEWS-3_0_0_md.html">NEWS-3.0.0</a>
    <li><a href="../NEWS/NEWS-3_1_0_md.html">NEWS-3.1.0</a>
    <li><a href="../NEWS/NEWS-3_2_0_md.html">NEWS-3.2.0</a>
    <li><a href="../bsearch_rdoc.html">bsearch</a>
    <li><a href="../bug_triaging_rdoc.html">bug_triaging</a>
    <li><a href="../case_mapping_rdoc.html">case_mapping</a>
    <li><a href="../character_selectors_rdoc.html">character_selectors</a>
    <li><a href="../command_injection_rdoc.html">command_injection</a>
    <li><a href="../contributing_md.html">contributing</a>
    <li><a href="../contributing/building_ruby_md.html">building_ruby</a>
    <li><a href="../contributing/documentation_guide_md.html">documentation_guide</a>
    <li><a href="../contributing/glossary_md.html">glossary</a>
    <li><a href="../contributing/making_changes_to_ruby_md.html">making_changes_to_ruby</a>
    <li><a href="../contributing/making_changes_to_stdlibs_md.html">making_changes_to_stdlibs</a>
    <li><a href="../contributing/reporting_issues_md.html">reporting_issues</a>
    <li><a href="../contributing/testing_ruby_md.html">testing_ruby</a>
    <li><a href="../dig_methods_rdoc.html">dig_methods</a>
    <li><a href="../distribution_md.html">distribution</a>
    <li><a href="../dtrace_probes_rdoc.html">dtrace_probes</a>
    <li><a href="../encodings_rdoc.html">encodings</a>
    <li><a href="../extension_ja_rdoc.html">extension.ja</a>
    <li><a href="../extension_rdoc.html">extension</a>
    <li><a href="../fiber_md.html">fiber</a>
    <li><a href="../format_specifications_rdoc.html">format_specifications</a>
    <li><a href="../globals_rdoc.html">globals</a>
    <li><a href="../implicit_conversion_rdoc.html">implicit_conversion</a>
    <li><a href="../keywords_rdoc.html">keywords</a>
    <li><a href="../maintainers_md.html">maintainers</a>
    <li><a href="../marshal_rdoc.html">marshal</a>
    <li><a href="../memory_view_md.html">memory_view</a>
    <li><a href="../optparse/argument_converters_rdoc.html">argument_converters</a>
    <li><a href="../optparse/creates_option_rdoc.html">creates_option</a>
    <li><a href="../optparse/option_params_rdoc.html">option_params</a>
    <li><a href="../optparse/tutorial_rdoc.html">tutorial</a>
    <li><a href="../packed_data_rdoc.html">packed_data</a>
    <li><a href="../ractor_md.html">ractor</a>
    <li><a href="../regexp_rdoc.html">regexp</a>
    <li><a href="../regexp/methods_rdoc.html">methods</a>
    <li><a href="../regexp/unicode_properties_rdoc.html">unicode_properties</a>
    <li><a href="../ruby_3_3_0_preview2/COPYING.html">COPYING</a>
    <li><a href="../ruby_3_3_0_preview2/COPYING_ja.html">COPYING.ja</a>
    <li><a href="../ruby_3_3_0_preview2/LEGAL.html">LEGAL</a>
    <li><a href="../ruby_3_3_0_preview2/NEWS_md.html">NEWS</a>
    <li><a href="../ruby_3_3_0_preview2/README_ja_md.html">README.ja</a>
    <li><a href="../ruby_3_3_0_preview2/README_md.html">README</a>
    <li><a href="../security_rdoc.html">security</a>
    <li><a href="../signals_rdoc.html">signals</a>
    <li><a href="../standard_library_rdoc.html">standard_library</a>
    <li><a href="../strftime_formatting_rdoc.html">strftime_formatting</a>
    <li><a href="../syntax_rdoc.html">syntax</a>
    <li><a href="../syntax/assignment_rdoc.html">assignment</a>
    <li><a href="../syntax/calling_methods_rdoc.html">calling_methods</a>
    <li><a href="../syntax/comments_rdoc.html">comments</a>
    <li><a href="../syntax/control_expressions_rdoc.html">control_expressions</a>
    <li><a href="../syntax/exceptions_rdoc.html">exceptions</a>
    <li><a href="../syntax/literals_rdoc.html">literals</a>
    <li><a href="../syntax/methods_rdoc.html">methods</a>
    <li><a href="../syntax/miscellaneous_rdoc.html">miscellaneous</a>
    <li><a href="../syntax/modules_and_classes_rdoc.html">modules_and_classes</a>
    <li><a href="../syntax/pattern_matching_rdoc.html">pattern_matching</a>
    <li><a href="../syntax/precedence_rdoc.html">precedence</a>
    <li><a href="../syntax/refinements_rdoc.html">refinements</a>
    <li><a href="../timezones_rdoc.html">timezones</a>
    <li><a href="../windows_md.html">windows</a>
    <li><a href="../yjit/yjit_md.html">yjit</a>
    <li><a href="../yjit/yjit_hacking_md.html">yjit_hacking</a>
  </ul>
</div>

   </div>
  </div>
</nav>


<!--  carbon ads here -->

<div id='extraz'>
  <div class='adzbox-index'  >
   
  </div>
 </div>         


<main role="main" aria-label="Page syntax/modules_and_classes.rdoc">

<h1 id="label-Modules">Modules<span><a href="#label-Modules">&para;</a> <a href="#top">&uarr;</a></span></h1>

<p>Modules serve two purposes in Ruby, namespacing and mix-in functionality.</p>

<p>A namespace can be used to organize code by package or functionality that separates common names from interference by other packages.  For example, the IRB namespace provides functionality for irb that prevents a collision for the common name “Context”.</p>

<p>Mix-in functionality allows sharing common methods across multiple classes or modules.  Ruby comes with the <a href="../Enumerable.html"><code>Enumerable</code></a> mix-in module which provides many enumeration methods based on the <code>each</code> method and <a href="../Comparable.html"><code>Comparable</code></a> allows comparison of objects based on the <code>&lt;=&gt;</code> comparison method.</p>

<p>Note that there are many similarities between modules and classes.  Besides the ability to mix-in a module, the description of modules below also applies to classes.</p>

<h2 id="label-Module+Definition"><a href="../Module.html"><code>Module</code></a> Definition<span><a href="#label-Module+Definition">&para;</a> <a href="#top">&uarr;</a></span></h2>

<p>A module is created using the <code>module</code> keyword:</p>

<pre class="ruby"><span class="ruby-keyword">module</span> <span class="ruby-constant">MyModule</span>
  <span class="ruby-comment"># ...</span>
<span class="ruby-keyword">end</span>
</pre>

<p>A module may be reopened any number of times to add, change or remove functionality:</p>

<pre class="ruby"><span class="ruby-keyword">module</span> <span class="ruby-constant">MyModule</span>
  <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">my_method</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>

<span class="ruby-keyword">module</span> <span class="ruby-constant">MyModule</span>
  <span class="ruby-keyword">alias</span> <span class="ruby-identifier">my_alias</span> <span class="ruby-identifier">my_method</span>
<span class="ruby-keyword">end</span>

<span class="ruby-keyword">module</span> <span class="ruby-constant">MyModule</span>
  <span class="ruby-identifier">remove_method</span> <span class="ruby-value">:my_method</span>
<span class="ruby-keyword">end</span>
</pre>

<p>Reopening classes is a very powerful feature of Ruby, but it is best to only reopen classes you own.  Reopening classes you do not own may lead to naming conflicts or difficult to diagnose bugs.</p>

<h2 id="label-Nesting">Nesting<span><a href="#label-Nesting">&para;</a> <a href="#top">&uarr;</a></span></h2>

<p>Modules may be nested:</p>

<pre class="ruby"><span class="ruby-keyword">module</span> <span class="ruby-constant">Outer</span>
  <span class="ruby-keyword">module</span> <span class="ruby-constant">Inner</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>
</pre>

<p>Many packages create a single outermost module (or class) to provide a namespace for their functionality.</p>

<p>You may also define inner modules using <code>::</code> provided the outer modules (or classes) are already defined:</p>

<pre class="ruby"><span class="ruby-keyword">module</span> <span class="ruby-constant">Outer</span><span class="ruby-operator">::</span><span class="ruby-constant">Inner</span><span class="ruby-operator">::</span><span class="ruby-constant">GrandChild</span>
<span class="ruby-keyword">end</span>
</pre>

<p>Note that this will raise a <code>NameError</code> if <code>Outer</code> and <code>Outer::Inner</code> are not already defined.</p>

<p>This style has the benefit of allowing the author to reduce the amount of indentation.  Instead of 3 levels of indentation only one is necessary. However, the scope of constant lookup is different for creating a namespace using this syntax instead of the more verbose syntax.</p>

<h2 id="label-Scope">Scope<span><a href="#label-Scope">&para;</a> <a href="#top">&uarr;</a></span></h2>

<h3 id="label-self"><code>self</code><span><a href="#label-self">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p><code>self</code> refers to the object that defines the current scope.  <code>self</code> will change when entering a different method or when defining a new module.</p>

<h3 id="label-Constants">Constants<span><a href="#label-Constants">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p>Accessible constants are different depending on the module nesting (which syntax was used to define the module).  In the following example the constant <code>A::Z</code> is accessible from B as A is part of the nesting:</p>

<pre class="ruby"><span class="ruby-keyword">module</span> <span class="ruby-constant">A</span>
  <span class="ruby-constant">Z</span> = <span class="ruby-value">1</span>

  <span class="ruby-keyword">module</span> <span class="ruby-constant">B</span>
    <span class="ruby-identifier">p</span> <span class="ruby-constant">Module</span>.<span class="ruby-identifier">nesting</span> <span class="ruby-comment">#=&gt; [A::B, A]</span>
    <span class="ruby-identifier">p</span> <span class="ruby-constant">Z</span> <span class="ruby-comment">#=&gt; 1</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>
</pre>

<p>However, if you use <code>::</code> to define <code>A::B</code> without nesting it inside <code>A</code>, a <a href="../NameError.html"><code>NameError</code></a> exception will be raised because the nesting does not include <code>A</code>:</p>

<pre class="ruby"><span class="ruby-keyword">module</span> <span class="ruby-constant">A</span>
  <span class="ruby-constant">Z</span> = <span class="ruby-value">1</span>
<span class="ruby-keyword">end</span>

<span class="ruby-keyword">module</span> <span class="ruby-constant">A</span><span class="ruby-operator">::</span><span class="ruby-constant">B</span>
  <span class="ruby-identifier">p</span> <span class="ruby-constant">Module</span>.<span class="ruby-identifier">nesting</span> <span class="ruby-comment">#=&gt; [A::B]</span>
  <span class="ruby-identifier">p</span> <span class="ruby-constant">Z</span> <span class="ruby-comment">#=&gt; raises NameError</span>
<span class="ruby-keyword">end</span>
</pre>

<p>If a constant is defined at the top-level you may preceded it with <code>::</code> to reference it:</p>

<pre class="ruby"><span class="ruby-constant">Z</span> = <span class="ruby-value">0</span>

<span class="ruby-keyword">module</span> <span class="ruby-constant">A</span>
  <span class="ruby-constant">Z</span> = <span class="ruby-value">1</span>

  <span class="ruby-keyword">module</span> <span class="ruby-constant">B</span>
    <span class="ruby-identifier">p</span> <span class="ruby-operator">::</span><span class="ruby-constant">Z</span> <span class="ruby-comment">#=&gt; 0</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>
</pre>

<h3 id="label-Methods">Methods<span><a href="#label-Methods">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p>For method definition documentation see the <a href="methods_rdoc.html">syntax documentation for methods</a>.</p>

<p><a href="../Class.html"><code>Class</code></a> methods may be called directly.  (This is slightly confusing, but a method on a module is often called a “class method” instead of a “module method”.  See also <a href="../Module.html#method-i-module_function"><code>Module#module_function</code></a> which can convert an instance method into a class method.)</p>

<p>When a class method references a constant, it uses the same rules as referencing it outside the method as the scope is the same.</p>

<p>Instance methods defined in a module are only callable when included.  These methods have access to the constants defined when they were included through the ancestors list:</p>

<pre class="ruby"><span class="ruby-keyword">module</span> <span class="ruby-constant">A</span>
  <span class="ruby-constant">Z</span> = <span class="ruby-value">1</span>

  <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">z</span>
    <span class="ruby-constant">Z</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>

<span class="ruby-identifier">include</span> <span class="ruby-constant">A</span>

<span class="ruby-identifier">p</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">ancestors</span> <span class="ruby-comment">#=&gt; [Object, A, Kernel, BasicObject]</span>
<span class="ruby-identifier">p</span> <span class="ruby-identifier">z</span> <span class="ruby-comment">#=&gt; 1</span>
</pre>

<h3 id="label-Visibility">Visibility<span><a href="#label-Visibility">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p>Ruby has three types of visibility.  The default is <code>public</code>.  A public method may be called from any other object.</p>

<p>The second visibility is <code>protected</code>.  When calling a protected method the sender must inherit the <a href="../Class.html"><code>Class</code></a> or <a href="../Module.html"><code>Module</code></a> which defines the method.  Otherwise a <a href="../NoMethodError.html"><code>NoMethodError</code></a> will be raised.</p>

<p>Protected visibility is most frequently used to define <code>==</code> and other comparison methods where the author does not wish to expose an object’s state to any caller and would like to restrict it only to inherited classes.</p>

<p>Here is an example:</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">A</span>
  <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">n</span>(<span class="ruby-identifier">other</span>)
    <span class="ruby-identifier">other</span>.<span class="ruby-identifier">m</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>

<span class="ruby-keyword">class</span> <span class="ruby-constant">B</span> <span class="ruby-operator">&lt;</span> <span class="ruby-constant">A</span>
  <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">m</span>
    <span class="ruby-value">1</span>
  <span class="ruby-keyword">end</span>

  <span class="ruby-identifier">protected</span> <span class="ruby-value">:m</span>

<span class="ruby-keyword">end</span>

<span class="ruby-keyword">class</span> <span class="ruby-constant">C</span> <span class="ruby-operator">&lt;</span> <span class="ruby-constant">B</span>
<span class="ruby-keyword">end</span>

<span class="ruby-identifier">a</span> = <span class="ruby-constant">A</span>.<span class="ruby-identifier">new</span>
<span class="ruby-identifier">b</span> = <span class="ruby-constant">B</span>.<span class="ruby-identifier">new</span>
<span class="ruby-identifier">c</span> = <span class="ruby-constant">C</span>.<span class="ruby-identifier">new</span>

<span class="ruby-identifier">c</span>.<span class="ruby-identifier">n</span> <span class="ruby-identifier">b</span> <span class="ruby-comment">#=&gt; 1 -- C is a subclass of B</span>
<span class="ruby-identifier">b</span>.<span class="ruby-identifier">n</span> <span class="ruby-identifier">b</span> <span class="ruby-comment">#=&gt; 1 -- m called on defining class</span>
<span class="ruby-identifier">a</span>.<span class="ruby-identifier">n</span> <span class="ruby-identifier">b</span> <span class="ruby-comment"># raises NoMethodError A is not a subclass of B</span>
</pre>

<p>The third visibility is <code>private</code>.  A private method may only be called from inside the owner class without a receiver, or with a literal <code>self</code> as a receiver.  If a private method is called with a receiver other than a literal <code>self</code>, a <a href="../NoMethodError.html"><code>NoMethodError</code></a> will be raised.</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">A</span>
  <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">without</span>
    <span class="ruby-identifier">m</span>
  <span class="ruby-keyword">end</span>

  <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">with_self</span>
    <span class="ruby-keyword">self</span>.<span class="ruby-identifier">m</span>
  <span class="ruby-keyword">end</span>

  <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">with_other</span>
    <span class="ruby-constant">A</span>.<span class="ruby-identifier">new</span>.<span class="ruby-identifier">m</span>
  <span class="ruby-keyword">end</span>

  <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">with_renamed</span>
    <span class="ruby-identifier">copy</span> = <span class="ruby-keyword">self</span>
    <span class="ruby-identifier">copy</span>.<span class="ruby-identifier">m</span>
  <span class="ruby-keyword">end</span>

  <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">m</span>
    <span class="ruby-value">1</span>
  <span class="ruby-keyword">end</span>

  <span class="ruby-identifier">private</span> <span class="ruby-value">:m</span>
<span class="ruby-keyword">end</span>

<span class="ruby-identifier">a</span> = <span class="ruby-constant">A</span>.<span class="ruby-identifier">new</span>
<span class="ruby-identifier">a</span>.<span class="ruby-identifier">without</span>      <span class="ruby-comment">#=&gt; 1</span>
<span class="ruby-identifier">a</span>.<span class="ruby-identifier">with_self</span>    <span class="ruby-comment">#=&gt; 1</span>
<span class="ruby-identifier">a</span>.<span class="ruby-identifier">with_other</span>   <span class="ruby-comment"># NoMethodError (private method `m&#39; called for #&lt;A:0x0000559c287f27d0&gt;)</span>
<span class="ruby-identifier">a</span>.<span class="ruby-identifier">with_renamed</span> <span class="ruby-comment"># NoMethodError (private method `m&#39; called for #&lt;A:0x0000559c285f8330&gt;)</span>
</pre>

<h3 id="label-alias+and+undef"><code>alias</code> and <code>undef</code><span><a href="#label-alias+and+undef">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p>You may also alias or undefine methods, but these operations are not restricted to modules or classes.  See the <a href="miscellaneous_rdoc.html">miscellaneous syntax section</a> for documentation.</p>

<h1 id="label-Classes">Classes<span><a href="#label-Classes">&para;</a> <a href="#top">&uarr;</a></span></h1>

<p>Every class is also a module, but unlike modules a class may not be mixed-in to another module (or class).  Like a module, a class can be used as a namespace. A class also inherits methods and constants from its superclass.</p>

<h2 id="label-Defining+a+class">Defining a class<span><a href="#label-Defining+a+class">&para;</a> <a href="#top">&uarr;</a></span></h2>

<p>Use the <code>class</code> keyword to create a class:</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">MyClass</span>
  <span class="ruby-comment"># ...</span>
<span class="ruby-keyword">end</span>
</pre>

<p>If you do not supply a superclass your new class will inherit from <a href="../Object.html"><code>Object</code></a>.  You may inherit from a different class using <code>&lt;</code> followed by a class name:</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">MySubclass</span> <span class="ruby-operator">&lt;</span> <span class="ruby-constant">MyClass</span>
  <span class="ruby-comment"># ...</span>
<span class="ruby-keyword">end</span>
</pre>

<p>There is a special class <a href="../BasicObject.html"><code>BasicObject</code></a> which is designed as a blank class and includes a minimum of built-in methods.  You can use <a href="../BasicObject.html"><code>BasicObject</code></a> to create an independent inheritance structure.  See the <a href="../BasicObject.html"><code>BasicObject</code></a> documentation for further details.</p>

<h2 id="label-Inheritance">Inheritance<span><a href="#label-Inheritance">&para;</a> <a href="#top">&uarr;</a></span></h2>

<p>Any method defined on a class is callable from its subclass:</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">A</span>
  <span class="ruby-constant">Z</span> = <span class="ruby-value">1</span>

  <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">z</span>
    <span class="ruby-constant">Z</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>

<span class="ruby-keyword">class</span> <span class="ruby-constant">B</span> <span class="ruby-operator">&lt;</span> <span class="ruby-constant">A</span>
<span class="ruby-keyword">end</span>

<span class="ruby-identifier">p</span> <span class="ruby-constant">B</span>.<span class="ruby-identifier">new</span>.<span class="ruby-identifier">z</span> <span class="ruby-comment">#=&gt; 1</span>
</pre>

<p>The same is true for constants:</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">A</span>
  <span class="ruby-constant">Z</span> = <span class="ruby-value">1</span>
<span class="ruby-keyword">end</span>

<span class="ruby-keyword">class</span> <span class="ruby-constant">B</span> <span class="ruby-operator">&lt;</span> <span class="ruby-constant">A</span>
  <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">z</span>
    <span class="ruby-constant">Z</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>

<span class="ruby-identifier">p</span> <span class="ruby-constant">B</span>.<span class="ruby-identifier">new</span>.<span class="ruby-identifier">z</span> <span class="ruby-comment">#=&gt; 1</span>
</pre>

<p>You can override the functionality of a superclass method by redefining the method:</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">A</span>
  <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">m</span>
    <span class="ruby-value">1</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>

<span class="ruby-keyword">class</span> <span class="ruby-constant">B</span> <span class="ruby-operator">&lt;</span> <span class="ruby-constant">A</span>
  <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">m</span>
    <span class="ruby-value">2</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>

<span class="ruby-identifier">p</span> <span class="ruby-constant">B</span>.<span class="ruby-identifier">new</span>.<span class="ruby-identifier">m</span> <span class="ruby-comment">#=&gt; 2</span>
</pre>

<p>If you wish to invoke the superclass functionality from a method use <code>super</code>:</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">A</span>
  <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">m</span>
    <span class="ruby-value">1</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>

<span class="ruby-keyword">class</span> <span class="ruby-constant">B</span> <span class="ruby-operator">&lt;</span> <span class="ruby-constant">A</span>
  <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">m</span>
    <span class="ruby-value">2</span> <span class="ruby-operator">+</span> <span class="ruby-keyword">super</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>

<span class="ruby-identifier">p</span> <span class="ruby-constant">B</span>.<span class="ruby-identifier">new</span>.<span class="ruby-identifier">m</span> <span class="ruby-comment">#=&gt; 3</span>
</pre>

<p>When used without any arguments <code>super</code> uses the arguments given to the subclass method.  To send no arguments to the superclass method use <code>super()</code>.  To send specific arguments to the superclass method provide them manually like <code>super(2)</code>.</p>

<p><code>super</code> may be called as many times as you like in the subclass method.</p>

<h1 id="label-Singleton+Classes">Singleton Classes<span><a href="#label-Singleton+Classes">&para;</a> <a href="#top">&uarr;</a></span></h1>

<p>The singleton class (also known as the metaclass or eigenclass) of an object is a class that holds methods for only that instance.  You can access the singleton class of an object using <code>class &lt;&lt; object</code> like this:</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">C</span>
<span class="ruby-keyword">end</span>

<span class="ruby-keyword">class</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-constant">C</span>
  <span class="ruby-comment"># self is the singleton class here</span>
<span class="ruby-keyword">end</span>
</pre>

<p>Most frequently you’ll see the singleton class accessed like this:</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">C</span>
  <span class="ruby-keyword">class</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-keyword">self</span>
    <span class="ruby-comment"># ...</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>
</pre>

<p>This allows definition of methods and attributes on a class (or module) without needing to write <code>def self.my_method</code>.</p>

<p>Since you can open the singleton class of any object this means that this code block:</p>

<pre class="ruby"><span class="ruby-identifier">o</span> = <span class="ruby-constant">Object</span>.<span class="ruby-identifier">new</span>

<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">o</span>.<span class="ruby-identifier">my_method</span>
  <span class="ruby-value">1</span> <span class="ruby-operator">+</span> <span class="ruby-value">1</span>
<span class="ruby-keyword">end</span>
</pre>

<p>is equivalent to this code block:</p>

<pre class="ruby"><span class="ruby-identifier">o</span> = <span class="ruby-constant">Object</span>.<span class="ruby-identifier">new</span>

<span class="ruby-keyword">class</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">o</span>
  <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">my_method</span>
    <span class="ruby-value">1</span> <span class="ruby-operator">+</span> <span class="ruby-value">1</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>
</pre>

<p>Both objects will have a <code>my_method</code> that returns <code>2</code>.</p>

</main>

</div>  <!--  class='wrapper hdiv' -->


<footer id="validator-badges" role="contentinfo">
<p><a href="https://validator.w3.org/check/referer">Validate</a></p>
<p>Generated by <a href="https://ruby.github.io/rdoc/">RDoc</a> 6.4.0.</p>
<p>Based on <a href="https://github.com/ged/darkfish/">Darkfish</a> by <a href="http://deveiate.org">Michael Granger</a>.</p>

  
    <p><p><a href="https://ruby-doc.org">Ruby-doc.org</a> is a service of <a href="https://jamesbritt.com">James Britt</a> and <a href="https://neurogami.com">Neurogami</a>, purveyors of fine <a href='https://jamesbritt.bandcamp.com/'>dance noise</a></p>
</p>
  
  </footer>

<script type="text/javascript">


  let ads  = $("#carbonads-container").children().detach();


  function swapMode() {
    var cookieName = 'darkmode';
    var cssDarkmode = Cookies.get(cookieName);
    console.log("***** swapMode! " + cssDarkmode + " *****");


    if (cssDarkmode == "true") {
      console.log("We have dark mode, set the css to light ...");
      $('#rdoccss').attr("href", defaultModeCssHref);
      $('#cssSelect').text("Dark mode");
      cssDarkmode = "false";
      console.log("swapMode! Now set cookie to " + cssDarkmode);
      Cookies.set(cookieName, cssDarkmode);

    } else {
      console.log("We not have dark mode, set the css to dark ...");
      $('#rdoccss').attr("href", darkModeCsseHref);
      $('#cssSelect').text("Light mode");
      cssDarkmode = "true";
      console.log("swapMode! Now set cookie to " + cssDarkmode);
      Cookies.set(cookieName, cssDarkmode);

    }

    console.log("  --------------- ");
  }


const vueCssApp = new Vue({
el: '#menubar',
data: {
isDark: false
},
methods: {
toggleClass: function(event){
this.isDark = !this.isDark;
}
}
})

const vueApp = new Vue({
el: '#vapp',
data: { 
isOpen: true
},

mounted() {
this.handleResize();
this.manage_mob_classes();
window.addEventListener('resize', this.handleResize)
//this.isOpen !=  (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent));
},
destroyed() {
window.removeEventListener('resize', this.handleResize)
},
created() {
//manage_mob_classes();
},

methods : {
isMobile() {
  return (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent));
},

  handleResize() {
    if (!this.isMobile()) {
      this.isOpen = window.innerWidth > 800;
    }
  },

  manage_mob_classes() {
    if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) {
      $("nav").addClass("mob_nav");
      $("main").addClass("mob_main");
      $("#extraz").addClass("mob_extraz");
      $("#carbonads-container").addClass("mob_carbonads-container");
      this.isOpen  = false;
    } else {
      $("nav").removeClass("mob_nav") 
        $("main").removeClass("mob_main");
      $("#extraz").removeClass("mob_extraz");
      $("#carbonads-container").removeClass("mob_carbonads-container");
      this.isOpen  = true;
    }
  },

  toggleNav() {
    this.isOpen =! this.isOpen ;
    // alert("Toggle nav!");
    console.log("toggleNav() click: " + this.isOpen );
  }
}
})

$("#carbonads-container").append(ads);


$(function() {

    var darkmode = Cookies.get("darkmode");
    console.log("Document ready: " + darkmode);

    if ( darkmode  == "true" ) {
      $('#cssSelect').text("Light mode");
    } else {
      $('#cssSelect').text("Dark mode");
     }

    $('body').css('display','block');
    });

</script>

    
  </body> 
</html>

